<?php
Yii::import('model.Cron');
Yii::import('model.CronHist');
Yii::import('lib.Util');
class JobController extends Controller {
    public function actionIndex(){
        $where = array();
        $uid = trim(Yii::app()->request->getParam('uid',0));
        $page_no  = Yii::app()->request->getParam('page_no',1);
        $search  = Yii::app()->request->getParam('search','');
        if ($uid) $where['uid'] = $uid ? $uid : $this->_uid;
        $page_size = PAGE_SIZE;
        $page_no=$page_no<1?1:$page_no;
        $info =Cron::getInstance()->getCronCount($uid);

        $row_count = $info['num'];
        $total_page=$row_count%$page_size==0?$row_count/$page_size:ceil($row_count/$page_size);
        $total_page=$total_page<1?1:$total_page;
        $page_no=$page_no>($total_page)?($total_page):$page_no;
        $start = ($page_no - 1) * $page_size;

        $page_str=Util::showPager("/job/?uid=$uid&search=1",$page_no,$page_size,$row_count);
        $cronList = Cron::getInstance()->getCronList($uid,$start,$page_size);

        $this->renderData['cronList']= $cronList;
        $this->renderData['page']= $page_str;
        $this->renderData['page_no'] = $page_no;
        $this->render('index',$this->renderData);
    }

    public function actionAdd(){
        $submit= Yii::app()->request->getParam('submit');
        if ($submit){
            $data["uid"] = $this->_uid;
            $data["name"] = trim(Yii::app ()->request->getParam ( 'name' ));
            $data["time"] = trim(Yii::app ()->request->getParam ( 'time' ));
            $data["ip"] = trim(Yii::app ()->request->getParam ( 'ip' ));
            $data["cmd"] = trim(Yii::app ()->request->getParam ( 'cmd' ));
            $data["sTime"] = trim(Yii::app ()->request->getParam ( 'sTime' ));
            $data["eTime"] = trim(Yii::app ()->request->getParam ( 'eTime' ));
            $data["process"] = trim(Yii::app ()->request->getParam ( 'process' ));
           // $data["modify"] = 1;
            try {
                if (Cron::getInstance()->add($data))
                {
                    $this->exitWithSuccess("添加任务成功", '/job/index');
                }else{
                    $this->exitWithError("添加任务失败", '/job/index');
                }
            } catch (Exception $e) {
                $this->exitWithError("添加任务失败", '/job/index');
            }
            return;
        }
        $this->render('add');
    }

    public function actionModify(){
        $id = trim(Yii::app()->request->getParam('id'));
        $page_no  = Yii::app()->request->getParam('page_no',1);
        $submit= Yii::app()->request->getParam('submit');
        if($submit && $id){
            $data["uid"] = $this->_uid;
            $data["name"] = trim(Yii::app ()->request->getParam ( 'name' ));
            $data["time"] = trim(Yii::app ()->request->getParam ( 'time' ));
            $data["ip"] = trim(Yii::app ()->request->getParam ( 'ip' ));
            $data["cmd"] = trim(Yii::app ()->request->getParam ( 'cmd' ));
            $data["sTime"] = strtotime(trim(Yii::app ()->request->getParam ( 'sTime' )));
            $data["eTime"] = strtotime(trim(Yii::app ()->request->getParam ( 'eTime' )));
            $data["process"] = trim(Yii::app ()->request->getParam ( 'process' ));
            $data["modify"] = 1;
            try {
                if (Cron::getInstance()->modify($data,$id))
                {
                    $this->exitWithSuccess("编辑任务成功", '/job/index?page_no='.$page_no);
                }else{
                    $this->exitWithError("添加任务失败", '/job/index?page_no='.$page_no);
                }
            } catch (Exception $e) {
                $this->exitWithError("添加任务失败", '/job/index?page_no='.$page_no);
            }
            return;
        }
        $this->renderData['id'] = $id;
        $this->renderData['cronInfo']  = Cron::getInstance()->getCronInfo($id);
        $this->render('modify',$this->renderData);
    }

    public function actionStop(){
        $id = trim(Yii::app()->request->getParam('id'));
        $page_no  = Yii::app()->request->getParam('page_no',1);
        $ret = Cron::getInstance()->modify(['modify'=>1,'status'=>0],$id);
        if($ret) {
            $this->exitWithSuccess("关闭任务成功", '/job/index?page='.$page_no);
        }else{
            $this->exitWithError("关闭任务失败", '/job/index?page='.$page_no);
        }
    }

    public function actionOpen(){
        $id = trim(Yii::app()->request->getParam('id'));
        $page_no  = Yii::app()->request->getParam('page_no',1);
        $ret = Cron::getInstance()->modify(['modify'=>1,'status'=>1],$id);
        if($ret) {
            $this->exitWithSuccess("打开任务成功", '/job/index?page='.$page_no);
        }else{
            $this->exitWithError("打开任务失败", '/job/index?page='.$page_no);
        }
    }

    public function actionHistory(){
        $id = trim(Yii::app()->request->getParam('cid'));
        if(!$id){
            exit("params error ");
        }
        $page_size = PAGE_SIZE;
        $page_no  = Yii::app()->request->getParam('page_no',1);
        $page_no=$page_no<1?1:$page_no;
        $row_count =CronHist::getInstance()->getCount($id);
        $total_page=$row_count%$page_size==0?$row_count/$page_size:ceil($row_count/$page_size);
        $total_page=$total_page<1?1:$total_page;
        $page_no=$page_no>($total_page)?($total_page):$page_no;
        $start = ($page_no - 1) * $page_size;
        $page_str=Util::showPager("/job/history?cid=$id&search=1",$page_no,$page_size,$row_count);
        $cronHist = CronHist::getInstance()->getCronHist($id,$start,$page_size);

        $this->renderData['cronHist']= $cronHist;
        $this->renderData['page']= $page_str;
        $this->renderData['page_no'] = $page_no;
        $this->render('history',$this->renderData);
    }

    public function actionAtOnce(){
        $id = trim(Yii::app()->request->getParam('id'));
        $page_no  = Yii::app()->request->getParam('page_no',1);
        $ret = Cron::getInstance()->modify(['atonce'=>1],$id);
        if($ret) {
            $this->exitWithSuccess("立即执行成功", '/job/index?page='.$page_no);
        }else{
            $this->exitWithError("立即执行失败", '/job/index?page='.$page_no);
        }
    }
}
?>